/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.repair.wrapper;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.repair.entity.MaintainPlanContent;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.vo.MaintainPlanContentVO;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 维保任务表包装类,返回视图层所需的字段
 *
 * @author ysq
 * @since 2022-03-09
 */
public class MaintainPlanContentWrapper extends BaseEntityWrapper<MaintainPlanContent, MaintainPlanContentVO>  {

	public static MaintainPlanContentWrapper build() {
		return new MaintainPlanContentWrapper();
 	}

	@Override
	public MaintainPlanContentVO entityVO(MaintainPlanContent maintainPlanContent) {
		MaintainPlanContentVO maintainPlanContentVO = Objects.requireNonNull(BeanUtil.copy(maintainPlanContent, MaintainPlanContentVO.class));
		maintainPlanContentVO.setStatusName(TaskStatusEnum.getTaskName(maintainPlanContent.getTypeStatus(),maintainPlanContent.getStatus()));
		return maintainPlanContentVO;
	}

	/**
     * 包装类，返回状态名称字段
	 * @param entity
     * @param <E>
     * @return
     */
	public <E> E convertEntityByStatus(E entity) {
		Field f_status = null;
		Field f_typeStatus = null;
		Field f_statusName = null;

		List<Field> fields = new ArrayList<>();
		Class entityClass = entity.getClass();
		while(entityClass != null) {
			fields.addAll(Arrays.asList(entityClass.getDeclaredFields()));
			// 父类
			entityClass = entityClass.getSuperclass();
		}

		for (Field field : fields) {
			String fileName = field.getName();
			switch (fileName) {
				case "status": {
					field.setAccessible(true);
					f_status = field;
					break;
				}
				case "typeStatus": {
					field.setAccessible(true);
					f_typeStatus = field;
					break;
				}
				case "statusName": {
					field.setAccessible(true);
					f_statusName = field;
					break;
				}
				default:{
					break;
				}
			}
		}

		if (f_status != null && f_typeStatus != null && f_statusName != null) {
			try {
				Integer status = Integer.valueOf(f_status.get(entity).toString());
				Integer typeStatus = Integer.valueOf(f_typeStatus.get(entity).toString());
				String statusName = TaskStatusEnum.getTaskName(typeStatus, status);
				f_statusName.set(entity, statusName);
			} catch (IllegalAccessException e) {
				return entity;
			}
		}

		return entity;
	}

	public <E> List<E> convertListByStatus(List<E> list) {
		return list.stream().map(entity -> convertEntityByStatus(entity)).collect(Collectors.toList());
	}

}
